/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Optimizations.java
 *
 * Created on Jun 7, 2011, 6:03:34 PM
 */
package eu.anorien.dmo.gui;

import eu.anorien.dmo.gui.optimization.Optimization;
import java.util.ArrayList;
import javax.swing.DefaultListModel;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class Optimizations extends javax.swing.JPanel {

    private ArrayList<Optimization> optimizations;
    private DefaultListModel listModel = new DefaultListModel();
    private MainFrame mainFrame;

    /** Creates new form Optimizations */
    public Optimizations(MainFrame frame, ArrayList<Optimization> optimizations) {
        this.mainFrame = frame;
        this.optimizations = optimizations;
        initComponents();
        optimizationsList.setModel(listModel);
    }

    public void updateOtherPanels() {
        mainFrame.update();
    }
    
    public void newOptimization(Optimization opt) {
        opt.setOptimizations(this);
        optimizationsPanel.removeAll();
        optimizationsPanel.add(opt);
        optimizations.add(opt);
        listModel.addElement(opt);
        optimizationsList.setSelectedValue(opt, true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        noOptLabel = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        optimizationsList = new javax.swing.JList();
        optimizationsPanel = new javax.swing.JPanel();

        noOptLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        noOptLabel.setText("Please select an active optimization");

        optimizationsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                optimizationsListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(optimizationsList);

        jSplitPane1.setLeftComponent(jScrollPane1);

        optimizationsPanel.setLayout(new javax.swing.BoxLayout(optimizationsPanel, javax.swing.BoxLayout.LINE_AXIS));
        jSplitPane1.setRightComponent(optimizationsPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 809, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void optimizationsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_optimizationsListValueChanged
        Optimization selected = (Optimization) optimizationsList.getSelectedValue();
        if (selected != null) {
            optimizationsPanel.removeAll();
            optimizationsPanel.add(selected);
        } else {
            optimizationsPanel.removeAll();
            optimizationsPanel.add(noOptLabel);
        }
    }//GEN-LAST:event_optimizationsListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel noOptLabel;
    private javax.swing.JList optimizationsList;
    private javax.swing.JPanel optimizationsPanel;
    // End of variables declaration//GEN-END:variables
}
